﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Ingr.SP3D.Reports.Middle;
using Ingr.SP3D.Common.Middle;
using Ingr.SP3D.Drawings.Middle;
using Ingr.SP3D.Common.Middle.Services.Hidden;
using Ingr.SP3D.Common.Middle.Services;
using Ingr.SP3D.Route.Middle;
using Ingr.SP3D.Support.Middle;
using Ingr.SP3D.Structure.Middle;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using Ingr.SP3D.ReferenceData.Middle;
using System.Reflection;
using Ingr.SP3D.Equipment.Middle;
using System.IO;
using SWS.PIP.DATA;

namespace SWS.PIP.Label
{
	/// <summary>
	/// 从排烟管的管段中，获取管段加工数据
	/// </summary>
	public class FabricateDataOfGasPipeSpoolLabel : QueryInterpreter
	{
		DataTable mDataTable = new DataTable();
		static string mGasPipeSpecs = "3CO6|3CSG6|3DSG6";

		public override DataTable Execute(string action, string argument)
		{
			//1 初始化表头
			mDataTable.Columns.Add("Data", typeof(String));

			//1.1 设计阶段时，直接返回表头
			if (EvaluateOnly)
				return mDataTable;

			//2 获取数据
			string sResultData = "";
			try
			{
				sResultData = GetFabricateDataFromPipePart();
			}
			catch (System.Exception ex)
			{
				//将错误显示到lable
				sResultData = ex.Message + "\n" + ex.StackTrace;
			}
			finally
			{
				DataRow row = mDataTable.NewRow();
				row.SetField("Data", sResultData);
				mDataTable.Rows.Add(row);
			}

			//3 返回数据
			return mDataTable;
		}

		/// <summary>
		/// 从管部件上获取加工信息
		/// </summary>
		/// <returns></returns>
		private string GetFabricateDataFromPipePart()
		{
			string sResult = "";

			if (InputObjects.Count == 0)
			{
				return "Input error.";
			}

			//类型核对
			PipeStockPart oPipeStockPart = InputObjects[0] as PipeStockPart;
			if (oPipeStockPart == null)
			{
				throw new Exception($"输入对象({InputObjects[0].ObjectID})不是管材部件。");
			}

			//判断材料等级是否为排烟管材料等级
			//RouteFeature oFeature = oPipeStockPart.Features[0];
			PipeRun oPipeRun = GetPipeRunFromPipePart(oPipeStockPart);
			string sSpecName = oPipeRun.Specification.SpecificationName;
			if (Regex.Matches(mGasPipeSpecs, sSpecName).Count == 0)
			{
				throw new Exception($"输入对象({InputObjects[0].ObjectID})不是排烟管材料等级部件({mGasPipeSpecs})。");
			}

			//获取数据
			DataTable fabDataDT = GasPipeFabData.GetGasPipeStockPartFabricateData(InputObjects[0]);
			sResult = CommonFunctions.GetLeftAlignReportFromDT(fabDataDT, new int[] {40, 6, 6, 6, 6 });

			return sResult;
		}

		/// <summary>
		/// 从管部件上获取所在PipeRun对象
		/// </summary>
		/// <param name="oBO"></param>
		/// <returns></returns>
		private PipeRun GetPipeRunFromPipePart(BusinessObject oBO)
		{
			if (!oBO.SupportsInterface("IJRtePipePart"))
			{
				throw new Exception($"对象（{oBO.ObjectID}）不是PipePart对象。");
			}

			ReadOnlyCollection<BusinessObject> oRels = oBO.GetRelationship("OwnsParts", "Owner").TargetObjects;
			if (oRels.Count == 0)
			{
				throw new Exception($"获取管部件（{oBO.ObjectID}）所在PipeRun失败，利用GetRelationship方法得到结果数量为0。");
			}

			return oRels[0] as PipeRun;
		}
	}
}
